package com.xiaofei.threadpool;

public class PThread extends Thread{
	private Runnable target;
	private boolean isIdle;
	private boolean isShutDown;
	private ThreadPool pool;
	@Override
	public void run() {
		synchronized (this) {
			while (!isShutDown) {
				isIdle = false;
				if (null != target) {
					// 执行runnable的run方法，后续代码需要等到run方法结束后才执行。区别于Thread的start方法
					target.run();
				}
				isIdle = true;
				try {
					pool.rePool(this);
					wait();
				} catch (Exception e) {
				}
			}
		}
	}
	
	public synchronized void shutdown(){
		this.isShutDown = true;
		notifyAll();
	}
	
	public boolean isIdle(){
		return this.isIdle;
	}
	
	public synchronized void setTarget(Runnable target){
		this.target = target;
		notifyAll();
	}
	public PThread(Runnable target,ThreadPool pool,String name){
		super(name);
		this.target = target;
		this.pool = pool;
	}
}
